#include "mode_corner_handle.h"
/*三角灯逻辑处理函数*/




ST_SYS gs_corner_sys;

void corner_power_off(SYS_OPT_KEY opt);
void corner_logo(SYS_OPT_KEY opt);
void corner_mode_1(SYS_OPT_KEY opt);
void corner_mode_2(SYS_OPT_KEY opt);
void corner_mode_3(SYS_OPT_KEY opt);
void corner_mode_4(SYS_OPT_KEY opt);

light_MODE corner_mode_handle[NUM_SYS_SSP]=
{
    {corner_power_off},
    {corner_logo},
    {corner_mode_1},
    {corner_mode_2},
    {corner_mode_3},
    {corner_mode_4},
};

EM_SYS_SSP app_logic_get_corner_recover_ssp()
{
    return gs_corner_sys.recssp;
}

void app_logic_set_corner_recover_ssp(EM_SYS_SSP ssp)
{
    if(ssp < NUM_SYS_SSP)
    {
        gs_corner_sys.recssp =ssp;
    }

    LOG_INFO("set corner recssp %d\n",ssp);
}

EM_SYS_SSP app_logic_get_corner_last_ssp()
{
    return gs_corner_sys.lastssp;
}

void app_logic_set_corner_last_ssp(EM_SYS_SSP ssp)
{
    if(ssp < NUM_SYS_SSP)
    {
        gs_corner_sys.lastssp =ssp;
    }

    LOG_INFO("set corner lastssp %d\n",ssp);
}

EM_SYS_SSP app_logic_get_corner_ssp()
{
    return gs_corner_sys.ssp;
}

void app_logic_set_corner_ssp(EM_SYS_SSP ssp)
{
    if(ssp < NUM_SYS_SSP)
    {
        gs_corner_sys.ssp =ssp;
    }

    LOG_INFO("set corner ssp %d\n",ssp);
}

SYS_OPT_KEY corner_mode_on_off_opt(EM_DRV_ACT key_act)
{
    SYS_OPT_KEY opt;
    if (key_act == DRV_ACT_KEY_SHORT)
    {
        if(app_logic_get_corner_ssp() == SSP_POWER_OFF) //判断当前状态
        {
            opt.oepation = OPT_POWER_TURNON; //开机
            opt.target = MODE_CORNER;
        }
        else if(app_logic_get_corner_ssp() != SSP_POWER_OFF)
        {
            opt.oepation = OPT_GO_NEXT; //下一步
            opt.target = MODE_CORNER;
        }
    }
    else if(key_act == DRV_ACT_KEY_2S) 
    {
        if(app_logic_get_corner_ssp() != SSP_POWER_OFF) //判断当前状态
        {
            opt.oepation = OPT_POWER_TURNOFF; //关机
            opt.target = MODE_CORNER;
        }
        else
        {
            opt.oepation = OPT_INVALID; //无效操作
            opt.target = MODE_CORNER;
        }
    }
    
    return opt;
}

void corner_power_off(SYS_OPT_KEY opt)
{
    if(app_logic_get_corner_last_ssp() != app_logic_get_corner_ssp())//如果是第一次进入
    {
        if(app_logic_get_corner_last_ssp() != SSP_LOGO) //恢复程序
        {
            app_logic_set_corner_recover_ssp(app_logic_get_corner_last_ssp()); //将上一次的状态保存
        }
        else
        {
            app_logic_set_corner_recover_ssp(SSP_MODE_1);
        }
        app_logic_set_corner_last_ssp(app_logic_get_corner_ssp()); //将上次的状态与本次的状态同步
        disp_clear_light_corner();
    }

    if (opt.target == MODE_CORNER)
    {
        if (opt.oepation == OPT_POWER_TURNON) ///开机操作
        {
            app_logic_set_corner_ssp(app_logic_get_corner_recover_ssp());
        }
    }
}

void corner_logo(SYS_OPT_KEY opt)
{

}

/*白光效果*/
void corner_mode_1(SYS_OPT_KEY opt)
{
    if(app_logic_get_corner_last_ssp() != app_logic_get_corner_ssp())//如果是第一次进入
    {
        app_logic_set_corner_last_ssp(app_logic_get_corner_ssp()); //将上次的状态与本次的状态同步
        disp_clear_light_corner(); // 清空前灯带的颜色

    }
    
    light_white_ws2812(CORNER_RIGHT_PORT,CORNER_LIGHT);
    light_white_ws2812(CORNER_LEFT_PORT,CORNER_LIGHT);

    if (opt.target == MODE_CORNER)
    {
        if (opt.oepation == OPT_GO_NEXT)
        {
            app_logic_set_corner_ssp(SSP_MODE_2);
        }
        else if(opt.oepation == OPT_POWER_TURNOFF)
        {
            app_logic_set_corner_ssp(SSP_POWER_OFF);
        }
    }
}


/*蓝光效果*/
void corner_mode_2(SYS_OPT_KEY opt)
{
    if(app_logic_get_corner_last_ssp() != app_logic_get_corner_ssp())//如果是第一次进入
    {
        app_logic_set_corner_last_ssp(app_logic_get_corner_ssp()); //将上次的状态与本次的状态同步
        disp_clear_light_corner(); // 清空前灯带的颜色

    }

    light_yellow_ws2812(CORNER_RIGHT_PORT,CORNER_LIGHT);
    light_yellow_ws2812(CORNER_LEFT_PORT,CORNER_LIGHT);

    if (opt.target == MODE_CORNER)
    {
        if (opt.oepation == OPT_GO_NEXT) //下一个
        {
            app_logic_set_corner_ssp(SSP_MODE_3);
        }
        else if(opt.oepation == OPT_POWER_TURNOFF) //关机
        {
            app_logic_set_corner_ssp(SSP_POWER_OFF);
        }
    }
}

/*蓝色呼吸灯*/
static void light_blue_breathing_right_ws2812(u8 port,u16 length,u8 clear)
{
    static u16 i =50;
    static u16 q = 0;
    static u8 dir = 1; // 设置颜色增减的方向，1为增加，0为减少
    if (clear == 1)
    {
        i = 50;
        q = 0;
        dir = 1;
    }

    q++;
    if(q > 5)
    {
        spi_send_param *spi_param = NULL;
        u8 *uart_rx_buffer = NULL;
        spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
        if (spi_param == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

        if(uart_rx_buffer == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }

        bzero(uart_rx_buffer,length*3);
        spi_param->send_len = length*3; //待发送数据长度
        spi_param->send_port = port; //待发送数据指定的灯带

        for (int j = 0; j <= length; j += 1)  //算出每个灯珠的颜色
        { 
            uart_rx_buffer[j * 3 + 0] = 0; //G
            uart_rx_buffer[j * 3 + 1] = 0; //R
            uart_rx_buffer[j * 3 + 2] = i; //B
        }

        if (dir == 1) {
            // 颜色从黑色变化到蓝色
            i=i+5;
            if(i==255)
            {
                dir = 0; // 改变方向为减少
            }

        } else {
            // 颜色从蓝色变化到黑色
            i=i-5;
            if(i==50)
            {
                dir = 1; // 改变方向为增加
            }
        }
        spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
        logic_send_light_msg(spi_param);
        uart_rx_buffer =NULL; //防止野指针
        spi_param = NULL;
         q=0;
    }
    
}

static void light_blue_breathing_left_ws2812(u8 port,u16 length,u8 clear)
{
    static u16 i =50;
    static u16 q = 0;
    static u8 dir = 1; // 设置颜色增减的方向，1为增加，0为减少
    if (clear == 1)
    {
        i =50;
        q = 0;
        dir = 1;
    }

    q++;
    if(q > 5)
    {
        spi_send_param *spi_param = NULL;
        u8 *uart_rx_buffer = NULL;
        spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
        if (spi_param == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

        if(uart_rx_buffer == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }

        bzero(uart_rx_buffer,length*3);
        spi_param->send_len = length*3; //待发送数据长度
        spi_param->send_port = port; //待发送数据指定的灯带

        for (int j = 0; j <= length; j += 1)  //算出每个灯珠的颜色
        { 
            uart_rx_buffer[j * 3 + 0] = 0; //G
            uart_rx_buffer[j * 3 + 1] = 0; //R
            uart_rx_buffer[j * 3 + 2] = i; //B
        }

        if (dir == 1) 
        {
            // 颜色从黑色变化到蓝色
            i=i+5;
            if(i==255)
            {
                dir = 0; // 改变方向为减少
            }
        } 
        else 
        {
            // 颜色从蓝色变化到黑色
            i=i-5;
            if(i==50)
            {
                dir = 1; // 改变方向为增加
            }
        }
        spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
        logic_send_light_msg(spi_param);
        uart_rx_buffer =NULL; //防止野指针
        spi_param = NULL;
         q=0;
    }
    
}


/*蓝色呼吸灯效果*/
void corner_mode_3(SYS_OPT_KEY opt)
{
    static u8 clear =0;
    if(app_logic_get_corner_last_ssp() != app_logic_get_corner_ssp())//如果是第一次进入
    {
        app_logic_set_corner_last_ssp(app_logic_get_corner_ssp()); //将上次的状态与本次的状态同步
        disp_clear_light_corner(); // 清空前灯带的颜色
        clear = 1;
    }

    if (clear == 1)
    {
        light_blue_breathing_right_ws2812(CORNER_RIGHT_PORT,CORNER_LIGHT,clear);
        light_blue_breathing_left_ws2812(CORNER_LEFT_PORT,CORNER_LIGHT,clear);
        clear = 0;
    }
    else
    {
        light_blue_breathing_right_ws2812(CORNER_RIGHT_PORT,CORNER_LIGHT,clear);
        light_blue_breathing_left_ws2812(CORNER_LEFT_PORT,CORNER_LIGHT,clear);
    }
    
    
    if (opt.target == MODE_CORNER)
    {
        if (opt.oepation == OPT_GO_NEXT) //下一个
        {
            app_logic_set_corner_ssp(SSP_MODE_4);
        }
        else if(opt.oepation == OPT_POWER_TURNOFF) //关机
        {
            app_logic_set_corner_ssp(SSP_POWER_OFF);
        }
    }
}

/*7彩灯光效果 右*/
static void light_rainbow_corner_right_ws2812(u8 port,u16 length,u8 clear)
{   
    static u16 j =0;
    static u16 q = 0;
    u8 buf[CORNER_LIGHT*3/3];
    if (clear == 1)
    {
        j=0;
        q=0;
    }

    q++;
    if(q > 5)
    {
        spi_send_param *spi_param = NULL;
        u8 *uart_rx_buffer = NULL;
        spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
        if (spi_param == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));
        if(uart_rx_buffer == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        bzero(uart_rx_buffer,length*3);
        spi_param->send_len = length*3; //待发送数据长度
        spi_param->send_port = port; //待发送数据指定的灯带

        for(uint16_t i=0; i < length*3/3; i=i+3) 
        {
            ws281x_set_pixel_color(i, ws281x_wheel((i+j)%255),length*3/3,buf);    //turn every third pixel on
        }

        for (size_t i = 0; i < CORNER_LIGHT/3; i++)
        {
            uart_rx_buffer[3*3*i+0] = buf[3*i+0];
            uart_rx_buffer[3*3*i+1] = buf[3*i+1];
            uart_rx_buffer[3*3*i+2] = buf[3*i+2];
            uart_rx_buffer[3*3*i+3] = buf[3*i+0];
            uart_rx_buffer[3*3*i+4] = buf[3*i+1];
            uart_rx_buffer[3*3*i+5] = buf[3*i+2];
            uart_rx_buffer[3*3*i+6] = buf[3*i+0];
            uart_rx_buffer[3*3*i+7] = buf[3*i+1];
            uart_rx_buffer[3*3*i+8] = buf[3*i+2];
        }



        spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
        logic_send_light_msg(spi_param);
        uart_rx_buffer =NULL; //防止野指针
        spi_param = NULL;

        q=0;
        j=j+1;
    }
    
    if(j > 255)
    {
        j=0;
    }

}

/*7彩灯光效果 左*/
static void light_rainbow_corner_left_ws2812(u8 port,u16 length,u8 clear)
{   
    static u16 j =0;
    static u16 q = 0;
    u8 buf[CORNER_LIGHT*3/3];
    if (clear == 1)
    {
        j=0;
        q=0;
    }

    q++;
    if(q > 5)
    {
        spi_send_param *spi_param = NULL;
        u8 *uart_rx_buffer = NULL;
        spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
        if (spi_param == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));
        if(uart_rx_buffer == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        bzero(uart_rx_buffer,length*3);
        spi_param->send_len = length*3; //待发送数据长度
        spi_param->send_port = port; //待发送数据指定的灯带

        for(uint16_t i=0; i < length*3/3; i=i+3) 
        {
            ws281x_set_pixel_color(i, ws281x_wheel((i+j)%255),length*3/3,buf);    //turn every third pixel on
        }

        for (size_t i = 0; i < CORNER_LIGHT/3; i++)
        {
            uart_rx_buffer[3*3*i+0] = buf[3*i+0];
            uart_rx_buffer[3*3*i+1] = buf[3*i+1];
            uart_rx_buffer[3*3*i+2] = buf[3*i+2];
            uart_rx_buffer[3*3*i+3] = buf[3*i+0];
            uart_rx_buffer[3*3*i+4] = buf[3*i+1];
            uart_rx_buffer[3*3*i+5] = buf[3*i+2];
            uart_rx_buffer[3*3*i+6] = buf[3*i+0];
            uart_rx_buffer[3*3*i+7] = buf[3*i+1];
            uart_rx_buffer[3*3*i+8] = buf[3*i+2];
        }

        spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
        logic_send_light_msg(spi_param);
        uart_rx_buffer =NULL; //防止野指针
        spi_param = NULL;

        q=0;
        j=j+1;
    }
    
    if(j > 255)
    {
        j=0;
    }

}

void corner_mode_4(SYS_OPT_KEY opt)
{
    static u8 clear = 0;
    if(app_logic_get_corner_last_ssp() != app_logic_get_corner_ssp())//如果是第一次进入
    {
        app_logic_set_corner_last_ssp(app_logic_get_corner_ssp()); //将上次的状态与本次的状态同步
        disp_clear_light_corner(); // 清空前灯带的颜色
        clear =1;
    }
    
    if(clear == 1)
    {
        light_rainbow_corner_right_ws2812(CORNER_RIGHT_PORT,CORNER_LIGHT,clear);
        light_rainbow_corner_left_ws2812(CORNER_LEFT_PORT,CORNER_LIGHT,clear);
        clear = 0;
    }
    else
    {
        light_rainbow_corner_right_ws2812(CORNER_RIGHT_PORT,CORNER_LIGHT,clear);
        light_rainbow_corner_left_ws2812(CORNER_LEFT_PORT,CORNER_LIGHT,clear);
    }


    if (opt.target == MODE_CORNER)
    {
        if (opt.oepation == OPT_GO_NEXT) //下一个
        {
            app_logic_set_corner_ssp(SSP_MODE_1);
        }
        else if(opt.oepation == OPT_POWER_TURNOFF) //关机
        {
            app_logic_set_corner_ssp(SSP_POWER_OFF);
        }
    }
}

void mode_corner_handle(SYS_OPT_KEY opt)
{
    if(corner_mode_handle[app_logic_get_corner_ssp()].handle)
    {
        corner_mode_handle[app_logic_get_corner_ssp()].handle(opt);
    }
    else
    {
        LOG_ERROR("corner cur ssp handle is not defined\n");
    }
}